import 'package:flutter/material.dart';

class XDAlertDialog extends Dialog {
  final String? title;
  late final String? content;
  final Widget? contentWidget;
  final String? confirm;
  final String? cancel;
  final Color? confirmColor;
  final Color? cancelColor;
  final Function()? confirmBlock;
  final Function()? cancelBlock;

  XDAlertDialog({
    Key? key,
    this.title,
    this.content,
    this.contentWidget,
    this.confirm,
    this.cancel,
    this.confirmColor,
    this.cancelColor,
    this.confirmBlock,
    this.cancelBlock,
  }): super(key: key){
    if(contentWidget != null) content = null;
  }

  @override
  Widget build(BuildContext context) {
    return Material(
      type: MaterialType.transparency,
      child: Center(
        child: Container(
          constraints: BoxConstraints(maxHeight: 300),
          width: 280,

          decoration: const ShapeDecoration(
              color: Colors.white,
              shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.all(Radius.circular(8.0)))
          ),

          child: Column(
            mainAxisAlignment: MainAxisAlignment.start,
            crossAxisAlignment: CrossAxisAlignment.center,
            mainAxisSize: MainAxisSize.min,
            children: [
              Container(
                margin: EdgeInsets.only(top: 20),
                height: 25,
                child: Center(
                  child: Text(title ?? '',style: TextStyle(
                    fontSize: 18,
                    fontWeight: FontWeight.bold,
                    color: Color(0xFF333333),
                  ),),
                ),
              ),

              Offstage(
                offstage: contentWidget == null,
                child: contentWidget,
              ),

              Offstage(
                  offstage: content == null,
                  child: Container(
                    margin: EdgeInsets.fromLTRB(0, 8, 0, 20),
                    child: Text(content??"",
                      style: TextStyle(
                          fontSize: 15,
                          color: Color(0xFF333333)
                      ),
                      textAlign: TextAlign.center,
                    ),
                  )
              ),

              if(cancel != null || confirm != null) Container(
                width: double.infinity,
                height: 0.5,
                color: Color(0xFFE5E5E5),
              ),

               if(cancel != null || confirm != null) _getBottomWidget(context),
            ],
          ),
        ),
      ),
    );
  }

  _getBottomWidget(BuildContext context){
    return Container(
      height: 49,
      child: Row(
        children: [
          if(cancel != null) Expanded(
            child: _customButton(
                title: cancel,
                titleColor: cancelColor??const Color(0xFF5C5A64),
                callBack: (){
                  Navigator.pop(context);
                  if(cancelBlock != null) cancelBlock!();
                }
            ),
          ),

          Offstage(
            offstage: cancel == null || confirm == null,
            child: Container(
              width: 0.5,
              height: 49,
              color: Color(0xFFE5E5E5),
            ),
          ),
          if(confirm != null) Expanded(
            child: _customButton(
                title: confirm,
                titleColor: confirmColor??const Color(0xFF0656EF),
                callBack: (){
                  Navigator.pop(context);
                  if(confirmBlock != null) confirmBlock!();
                }
            ),
          ),

        ],
      ),
    );
  }

  _customButton({
    String? title,
    Color titleColor = const Color(0xFF5C5A64),
    Function()? callBack,
  })  {
    return InkWell(
      onTap: callBack ,
      child: Container(
        alignment: Alignment.center,
        child: Text(
          title??"",
          style: TextStyle(
            fontSize: 14,
            color: titleColor,
          ),
        ),
      ),
    );
  }
}